<html>
<head><meta charset="utf-8"><title>How empty lang item trait works internally? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html">How empty lang item trait works internally?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="182591314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182591314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Derek <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182591314">(Dec 04 2019 at 19:40)</a>:</h4>
<p>Hi I'm trying to understand how How empty lang item trait works internally? e.g.<br>
src/libcore/marker.rs<br>
#[lang = "copy"]<br>
pub trait Copy : Clone {<br>
    // Empty.<br>
}<br>
for my understanding, this will eventually, translate to <br>
src/libcore/intrinsics.rs<br>
#[inline]<br>
pub unsafe fn copy&lt;T&gt;(src: *const T, dst: *mut T, count: usize) {<br>
    extern "rust-intrinsic" {<br>
        fn copy&lt;T&gt;(src: *const T, dst: *mut T, count: usize);<br>
    }</p>
<div class="codehilite"><pre><span></span>debug_assert!(is_aligned_and_not_null(src), &quot;attempt to copy from unaligned or null pointer&quot;);
debug_assert!(is_aligned_and_not_null(dst), &quot;attempt to copy to unaligned or null pointer&quot;);
copy(src, dst, count)
</pre></div>


<p>}<br>
then to src/librustc_codegen_llvm/intrinsic.rs<br>
fn copy_intrinsic(<br>
    bx: &amp;mut Builder&lt;'a, 'll, 'tcx&gt;,<br>
   ...<br>
) {<br>
...<br>
}<br>
But I want know how this happened? is this related to <br>
src/librustc/middle/lang_items.rs<br>
    CopyTraitLangItem,           "copy",               copy_trait,              Target::Trait;<br>
if my understanding is correct, but I want to know more about this process, where should I get started? can anyone give some clue? thanks a lot.</p>



<a name="182591940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182591940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182591940">(Dec 04 2019 at 19:48)</a>:</h4>
<p>the copy trait does not generate any code. It solely exists for the type system</p>



<a name="182591958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182591958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182591958">(Dec 04 2019 at 19:48)</a>:</h4>
<p>the <code>copy</code> intrinsic is unrelated to the copy trait</p>



<a name="182591972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182591972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182591972">(Dec 04 2019 at 19:48)</a>:</h4>
<p>they have no connection</p>



<a name="182592490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182592490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Derek <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182592490">(Dec 04 2019 at 19:54)</a>:</h4>
<p>@oli, Thanks a lots,  I really appreciate! Then if I have struct Person {},  then, have Person alice, then, let bob = alice.  this "=" here, need generate some code to do  bitwise copy, right? how does that works internally. e.g. after I #[derive(Copy, Clone)] or,  I have impl Copy for Person {}, this is again an empty Copy.</p>



<a name="182592553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182592553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182592553">(Dec 04 2019 at 19:55)</a>:</h4>
<p>it uses llvm assignments, which LLVM may make arbitrary decisions on how to translate</p>



<a name="182592645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182592645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182592645">(Dec 04 2019 at 19:56)</a>:</h4>
<p>you can observe and play with this on the playground (<a href="https://play.rust-lang.org/" target="_blank" title="https://play.rust-lang.org/">https://play.rust-lang.org/</a>) by using the <code>LLVM-IR</code> and <code>asm</code> buttons under the <code>...</code> menu in the left top corner</p>



<a name="182592922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182592922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Derek <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182592922">(Dec 04 2019 at 19:59)</a>:</h4>
<p>thanks, let me try,  Then why we need a empty statement here, like  impl Copy for Person {}.  is that just a rule? to let compiler know we allow it to do bitwise copy?  it I want dig into how compiler use this statement,  do you happen to know which crate or source file I should look into.</p>



<a name="182597524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182597524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Poveda <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182597524">(Dec 04 2019 at 20:49)</a>:</h4>
<p>yes, Copy just tells the compiler that the type can be copied implicitly</p>



<a name="182598350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182598350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182598350">(Dec 04 2019 at 20:57)</a>:</h4>
<p>this is to make sure you can do <code>let x = y; let z = y;</code> without erroring that <code>y</code> has been moved. The <code>Copy</code> trait has semantic value, but does not change what happens on the bits on the machine</p>



<a name="182599399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182599399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Derek <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182599399">(Dec 04 2019 at 21:07)</a>:</h4>
<p>Got it. Thanks, @oli, <span class="user-mention" data-user-id="132916">@Christian Poveda</span></p>



<a name="182600727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182600727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Derek <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182600727">(Dec 04 2019 at 21:19)</a>:</h4>
<p>Is implementation of  the rule related to this piece  of source code? <br>
src/librustc/middle/lang_items.rs<br>
language_item_table! {<br>
...<br>
    CopyTraitLangItem,           "copy",               copy_trait,              Target::Trait;<br>
}</p>



<a name="182602863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20empty%20lang%20item%20trait%20works%20internally%3F/near/182602863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20empty.20lang.20item.20trait.20works.20internally.3F.html#182602863">(Dec 04 2019 at 21:40)</a>:</h4>
<p>That's "just" an index of all the lang items. The actual logic can be found by grepping for <code>copy_trait()</code> and <code>CopyTraitLangItem</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>